package cn.tedu.ivos2.application.service.impl;

import cn.tedu.ivos2.application.mapper.ApplicationMapper;
import cn.tedu.ivos2.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivos2.application.pojo.dto.ApplicationSaveParam;
import cn.tedu.ivos2.application.pojo.entity.Application;
import cn.tedu.ivos2.application.pojo.vo.ApplicationVO;
import cn.tedu.ivos2.application.service.ApplicationService;
import cn.tedu.ivos2.audit.mapper.AuditMapper;
import cn.tedu.ivos2.audit.pojo.vo.AuditVO;
import cn.tedu.ivos2.audit.service.AuditService;
import cn.tedu.ivos2.base.enums.ApplicationStatusEnum;
import cn.tedu.ivos2.user.mapper.UserMapper;
import cn.tedu.ivos2.user.pojo.vo.UserVO;
import cn.tedu.ivos2.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos2.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

@Transactional
@Slf4j
@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private AuditService auditService;
    @Autowired
    private AuditMapper auditMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public void saveApplication(ApplicationSaveParam applicationSaveParam) {
        log.debug("保存用车申请:applicationSaveParam={}",applicationSaveParam);
        Application application = new Application();
        BeanUtils.copyProperties(applicationSaveParam,application);
        application.setCreateTime(new Date());
        application.setStatus(ApplicationStatusEnum.PENDING.getCode());
        applicationMapper.insert(application);
        auditService.insertAudit(application);
    }

    @Override
    public List<ApplicationVO> selectApplication(ApplicationQuery applicationQuery){
        log.debug("查询用车申请:applicationQuery={}",applicationQuery);
        List<ApplicationVO> list = applicationMapper.selectApplication(applicationQuery);
        for(ApplicationVO applicationVO:list){
            assignAuditUserList(applicationVO);
        }
        return list;
    }
    private void assignAuditUserList(ApplicationVO applicationVO){
        List<Long> auditUserIdList = new ArrayList<>();
        List<String> auditUsernameList = new ArrayList<>();
        List<AuditVO> auditVOList =
                auditMapper.selectAuditByApplicationId(applicationVO.getId());
        for(AuditVO auditVO:auditVOList){
            auditUserIdList.add(auditVO.getAuditUserId());
            Long id = auditVO.getAuditUserId();
            UserVO userVO = userMapper.selectById(id);
            auditUsernameList.add(userVO.getUsername());
        }
        StringJoiner sj = new StringJoiner(",");
        for(String username:auditUsernameList){
            sj.add(username);
        }
        applicationVO.setAuditUserIdList(auditUserIdList);
        applicationVO.setAuditUsernameList(sj.toString());
    }

    @Override
    public void cancel(Long id) {
        log.debug("取消申请业务:id={}",id);
        Application application = new Application();
        application.setStatus(ApplicationStatusEnum.CANCEL.getCode());
        application.setId(id);
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        auditMapper.deleteByApplicationId(id);
    }

    @Override
    public void distribute(Long applicationId, Long vehicleId) {
        log.debug("分配车辆业务:applicationId={},vehicleId={}",applicationId,vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(vehicleId);
        application.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);

        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("2");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }

    @Override
    public void back(Long applicationId, Long vehicleId) {
        log.debug("归还车辆业务:applicationId={},vehicleId={}",applicationId,vehicleId);
        Application application = new Application();
        application.setId(applicationId);
        application.setVehicleId(null);
        application.setStatus(ApplicationStatusEnum.END.getCode());
        application.setUpdateTime(new Date());
        applicationMapper.update(application);
        //还车需要把车辆状态改为空闲
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setStatus("1");

        vehicleMapper.update(vehicle);
    }
}
